package com.sample.dao;

import com.sample.entity.Company;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author Keyon
 * @Date 2019/12/1 22:02
 * @Desc 公司 - 持久层
 */
@Repository
public interface CompanyDao {

    @Select("select * from company")
    List<Company> findAllCompany();

    @Select("select * from company where id = #{id}")
    Company findCompanyById(Integer id);

    Company findCompanyWithUserById(Integer id);

    void saveCompany(Company company);

    /*@Select("SELECT c.id cid, companyName, u.id uid, u.username, u.email " +
            "FROM company c LEFT JOIN user u ON c.id = u.companyId WHERE c.id=#{id}")*/
    @Select("SELECT * FROM company WHERE id=#{id}")
    @Results(value = {
            @Result(property = "id", column = "id"),
            @Result(property = "companyName", column = "companyName"),
            @Result(property = "users", column = "id",
                    many = @Many(select = "com.sample.dao.UserDao.findUserByCompanyId"))})
    Company findCompanyWithUserById_v2(Integer id);

}
